//
//  ChooseControllerTool.m
//  WB
//
//  Created by lanou on 15/12/9.
//  Copyright (c) 2015年 ZL. All rights reserved.
//

#import "ChooseControllerTool.h"
#import "ICSDrawerController.h"
#import "GSRootTabBarController.h"
#import "GSMainViewController.h"
#import "GSRootViewController.h"
#import "GSNewFeatureViewController.h"
#import "GSStartADView.h"
#import "GSStartADModel.h"
#import "GSAdManager.h"
@implementation ChooseControllerTool

+ (void)chooseRootController
{
    NSString *versionKey = (__bridge NSString *)kCFBundleVersionKey;
    
    // 1.从沙盒中取出上一次存储的软件版本号(取出用户上一次的使用记录)
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *lastVersion = [defaults objectForKey:versionKey];
    
    // 2.取出当前的版本号
    NSDictionary *infoDic = [NSBundle mainBundle].infoDictionary;
    NSString *currentVersion = infoDic[versionKey];
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    if ([lastVersion isEqualToString: currentVersion]) {
        // 版本一致不展示新特性;
        
        GSRootTabBarController *tab = [[GSRootTabBarController alloc]init];
        
        GSRootViewController *root = [[GSRootViewController alloc]init];
        
        ICSDrawerController *ics = [[ICSDrawerController alloc]initWithLeftViewController:root centerViewController:tab];

        window.rootViewController = ics;
        
        
        GSStartADModel *model = [[GSAdManager shareManager]loadLatestAdImage];
        
        if ([GSAdManager isShouldDisplayAd]) {
            
            GSStartADView *adView = [[GSStartADView alloc]initWithFrame:[UIScreen mainScreen].bounds];
            
            adView.AdImage = [GSAdManager getAdImage];
            
            adView.model = model;
            
            adView.alpha = 0.99f;
            
            
            [[UIApplication sharedApplication].keyWindow addSubview:adView];
            
            [[UIApplication sharedApplication].keyWindow bringSubviewToFront:adView];
            
            [UIView animateWithDuration:3 animations:^{
                
                adView.alpha = 1.0f;
                
            } completion:^(BOOL finished) {
                
                [[UIApplication sharedApplication] setStatusBarHidden:NO];
                
                [UIView animateWithDuration:0.5 animations:^{
                    
                    adView.alpha = 0.0f;
                    
                } completion:^(BOOL finished) {
                    [adView removeFromSuperview];
                    
                    
                    
                }];
                //            [[NSNotificationCenter defaultCenter]postNotificationName:@"SXAdvertisementKey" object:nil];
            }];
            
        }else{
            //        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"update"];
        }

        
        
    } else {
        // 版本不一致展示新特性;
        window.rootViewController = [[GSNewFeatureViewController alloc]init];
        
        // 存储新版本号;
        [defaults setObject:currentVersion forKey:versionKey];
        [defaults synchronize];
    }

}
//
///**
// *  加载广告
// */
//- (void)initAdView
//{
//    
//}


@end
